package com.xckj.teacher.presenter

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.ToastUtils
import com.xckj.teacher.bean.BaseBean
import com.xckj.teacher.bean.ContactBean
import com.xckj.teacher.bean.UserInfoBean
import com.xckj.teacher.framework.IBaseView
import com.xckj.teacher.framework.net.RetrofitManager
import com.xckj.teacher.global.Constant
import com.xckj.teacher.ui.App
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Objects

class MinePresenter(private val iBaseView: IBaseView) {

    var userInfoData = MutableLiveData<UserInfoBean>()
    fun feedBack(content: String, success: () -> Unit) {
        val call = RetrofitManager.getInstance().service.feedBack(content)
        call.enqueue(object : Callback<BaseBean<Any?>> {
            override fun onResponse(
                call: Call<BaseBean<Any?>>,
                response: Response<BaseBean<Any?>>
            ) {
                if (response.isSuccessful) {
                    ToastUtils.showShort("投诉成功")
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<Any?>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
            }
        })
    }

    fun sendSms(mobile: String, type: String, success: () -> Unit) {
        val call = RetrofitManager.getInstance().service.sendSms(mobile, type)
        call.enqueue(object : Callback<BaseBean<Any?>> {
            override fun onResponse(
                call: Call<BaseBean<Any?>>,
                response: Response<BaseBean<Any?>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<Any?>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
            }
        })
    }

    fun changeMobile(
        mobile: String,
        mobileCap: String,
        captchaToken: String,
        captcha: String,
        success: () -> Unit
    ) {
        val call = RetrofitManager.getInstance().service.changeMobile(
            mobile,
            mobileCap,
            captchaToken,
            captcha
        )
        call.enqueue(object : Callback<BaseBean<Any?>> {
            override fun onResponse(
                call: Call<BaseBean<Any?>>,
                response: Response<BaseBean<Any?>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<Any?>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
            }
        })
    }


    fun connectUs(success: (ContactBean?) -> Unit) {
        val call = RetrofitManager.getInstance().service.connectUs()
        call.enqueue(object : Callback<BaseBean<ContactBean?>> {
            override fun onResponse(
                call: Call<BaseBean<ContactBean?>>,
                response: Response<BaseBean<ContactBean?>>
            ) {
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success(baseBean.data)
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<ContactBean?>>, t: Throwable) {
                t.printStackTrace()
                ToastUtils.showShort("请求失败")
            }
        })
    }

    fun resetPass(old: String, new: String, success: () -> Unit) {
        iBaseView.showLoading()
        val call = RetrofitManager.getInstance().service.resetpwd(old, new, new)
        call.enqueue(object : Callback<BaseBean<Any?>> {
            override fun onResponse(
                call: Call<BaseBean<Any?>>,
                response: Response<BaseBean<Any?>>
            ) {
                iBaseView.hideLoading()
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        success()
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<Any?>>, t: Throwable) {
                t.printStackTrace()
                iBaseView.hideLoading()
                ToastUtils.showShort("请求失败${t.message}")
            }
        })
    }

}
